﻿using Microsoft.EntityFrameworkCore;

namespace Application.Service
{
    public class EmailService : IEmailService
    {
        public JobManagerDbContext _context { get; set; }
        public EmailService(JobManagerDbContext DbContext)
        {
            _context = DbContext;
        }
        public async Task<bool> Send(SendEmailInfo sendEmailInfo)
        {
            var setting = await _context.KeyValue.Where(s => s.Type == KeyValueType.SettingNotice).ToListAsync();
            var emailServer = setting.FirstOrDefault(s => s.Key.ToLower() == Enum.GetName(SettingKey.EmailServer).ToLower());
            var emailServerArr = emailServer.Value.Split("$");

            var sysEmail = new SysEmail(emailServerArr[0], Convert.ToInt32(emailServerArr[1]), emailServerArr[2], emailServerArr[3]);
            var isSuccess = sysEmail.Send(sendEmailInfo.Subject, sendEmailInfo.BodyInfo, sendEmailInfo.ToEmails, IsBodyHtml: sendEmailInfo.IsBodyHtml).Result;
            return true;
        }

    }

    public interface IEmailService
    {
        Task<bool> Send(SendEmailInfo sendEmailInfo);
    }

    public class SendEmailInfo
    {
        public string Subject { get; set; }
        public string BodyInfo { get; set; }
        public string ToEmails { get; set; }
        public bool IsBodyHtml { get; set; } = true;
    }
}
